package com.baor.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.EquipmentDao;
import com.baor.dao.Equipment_alertDao;
import com.baor.domain.AlertVO;
import com.baor.domain.Equipment;
import com.baor.domain.Equipment_alert;
import com.baor.domain.RemovalRecordVO;
import com.baor.service.AlertService;
import com.baor.util.R;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 设备预警
@Service
public class AlertServiceImpl implements AlertService {

    @Autowired
    private Equipment_alertDao Equipment_alertDao;

    // 日期时间格式化器
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public R getList(Map<String, Object> params) {
        Page page = new Page();
        page.setCurrent((Integer) params.get("pageIndex"));
        page.setSize((Integer) params.get("pageSize"));
        MPJLambdaWrapper<Equipment_alert> wrapper = new MPJLambdaWrapper<>(Equipment_alert.class)
                .select(Equipment_alert::getAlertDescription,
                        Equipment_alert::getAlertType,
                        Equipment_alert::getAlertStatus,

                        Equipment_alert::getAlertTime)
                .select(Equipment::getCode,
                        Equipment::getAddress,
                        Equipment::getNowWeight,
                        Equipment::getThreshold)
                .leftJoin(Equipment.class, Equipment::getId,Equipment_alert::getEquipmentId)
                .eq(Equipment::getIsDeleted, 0);
        String equipmentCode = (String) params.get("code");
        String address = (String) params.get("address");
        Integer alertStatus = (Integer) params.get("alertStatus");
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        // 添加设备编号
        if (StringUtils.hasText(equipmentCode)) {
            wrapper.like(Equipment::getCode, equipmentCode);
        }
        // 添加设备状态
        if (alertStatus != null) {
            wrapper.like(Equipment_alert::getAlertStatus, alertStatus);
        }
        // 添加设备名称
        if (StringUtils.hasText(address)) {
            wrapper.like(Equipment::getAddress, address);
        }

        // 开始时间
        if (StringUtils.hasText(startTime)) {
            // 大于等于开始时间
            wrapper.ge(Equipment_alert::getAlertTime, startTime);
        }
        //结束时间
        if (StringUtils.hasText(endTime)) {
            // 小于等于结束时间
            wrapper.le(Equipment_alert::getAlertTime, endTime);
        }
        //TODO orderbydesc是用于降序排序的，而orderbyasc或orderby默认用于升序排序。
       // wrapper.orderByDesc(Equipment_alert::getAlertTime);
        Page<AlertVO> pageList = Equipment_alertDao.selectJoinPage(page, AlertVO.class, wrapper);
        Map map = new HashMap();
        map.put("totalSize",pageList.getTotal());
        map.put("list",pageList);
        return R.ok(map);
    }
}